--运行有问题的试试改过的代码
-- 推箱子

-- Use this function to perform your initial setup
displayMode(FULLSCREEN)
supportedOrientations(LANDSCAPE_RIGHT)

function setup()
    start = 0
    playing = 1
    win = 2
    gameState = playing
    left = ImgButton(80,200,"Cargo Bot:Command Left")
    left.clickedEvent = MoveLeft
    right = ImgButton(180,200,"Cargo Bot:Command Right")
    right.clickedEvent = MoveRight
    down = ImgButton(130,130,"Cargo Bot:Command Grab")
    down.clickedEvent = MoveDown
    up = ImgButton(130,270,"Cargo Bot:Condition Any")
    up.clickedEvent = MoveUp
    map1,map2 = {},{}
    block = {
    1,2,1,3,1,4,1,5,2,2,2,5,2,6,3,2,3,6,4,2,4,6,4,7,5,2,5,7,6,1,6,2,6,5,6,7,
    7,1,7,5,7,7,8,1,8,7,9,1,9,2,9,3,9,5,9,6,9,7,10,3,10,4,10,5
    }
    goal = {2,3,2,4,3,3,3,4,3,5}
    box = {4,4,5,5,5,3,6,4,7,3}
    pos = vec2(9,4)
    mapwidth = 10
    mapheight = 7
    for i =1,mapwidth do
        map1[i] = {}
        map2[i] = {}
        for j =1, mapheight do
            map1[i][j] = 0
            map2[i][j] = 0
        end
    end
    for i=1,#block,2 do
        map1[block[i]][block[i+1]] = 1
    end
    for i=1,#goal,2 do
        map1[goal[i]][goal[i+1]] = 2
    end
    for i=1,#box,2 do
        map2[box[i]][box[i+1]] =1
    end
    cellsize = 50
    --[[
    mapImage = image(500,350)
    setContext(mapImage)
    rectMode(CENTER)
    for i=1,mapwidth do
    for j=1,mapheight do
    if map1[i][j] == 1 then
    fill(238, 26, 5, 255)
    rect(i*cellsize-cellsize/2,j*cellsize-cellsize/2,cellsize)
elseif map1[i][j] == 2 then
    sprite("Platformer Art:Coin",i*cellsize-cellsize/2,j*cellsize-cellsize/2,cellsize)
end
end
end
    setContext(mapImage)
    --]]
    spriteMode(CORNER)
end

-- This function gets called once every frame
function draw()
    -- This sets a dark background color
    background(40, 40, 50)
    if gameState == playing then
        pushMatrix()
        translate(300,200)
        -- sprite(mapImage,0,0)
        for i=1,mapwidth do
            for j=1,mapheight do
                if map1[i][j] == 1 then
                    fill(238, 26, 5, 255)
                    rect(i*cellsize-cellsize,j*cellsize-cellsize,cellsize)
                elseif map1[i][j] == 2 then
                    sprite("Platformer Art:Coin",i*cellsize-cellsize,j*cellsize-cellsize,cellsize)
                end
            end
        end
        sprite("Planet Cute:Character Horn Girl",(pos.x-1)*cellsize,(pos.y-1)*cellsize,cellsize)
        for i=1,mapwidth do
            for j=1,mapheight do
                if map2[i][j] ==1 and map1[i][j] == 2 then
                    sprite("Cargo Bot:Crate Blue 2",(i-1)*cellsize,(j-1)*cellsize,cellsize)
                elseif map2[i][j] == 1 then
                    sprite("Cargo Bot:Crate Yellow 2",(i-1)*cellsize,(j-1)*cellsize,cellsize)
                end
            end
        end
        popMatrix()
        left:draw()
        right:draw()
        up:draw()
        down:draw()
    elseif gameState == win then
        fontSize(50)
        fill(255)
        text("恭喜你，过关！！！",WIDTH/2,HEIGHT/2)
    end
end
function touched(touch)
    left:touched(touch)
    right:touched(touch)
    up:touched(touch)
    down:touched(touch)
end
function MoveLeft()
    if map1[pos.x-1][pos.y] == 1 then return end
    if map2[pos.x-1][pos.y] ==1 then
        if map2[pos.x-2][pos.y] ==1 or map1[pos.x-2][pos.y] == 1 then return end
        if map1[pos.x-2][pos.y] == 0 or map1[pos.x-2][pos.y] == 2 then map2[pos.x-2][pos.y]=1 map2[pos.x-1][pos.y]=0 pos=pos+vec2(-1,0) GameWinCheck() return end
    end
    if map2[pos.x-1][pos.y] ==0 then
        if map1[pos.x-1][pos.y] == 0 or map1[pos.x-1][pos.y] ==2 then pos = pos +vec2(-1,0) end end
end

function MoveRight()
    if map1[pos.x+1][pos.y] == 1 then return end
    if map2[pos.x+1][pos.y] ==1 then
        if map2[pos.x+2][pos.y] ==1 or map1[pos.x+2][pos.y] == 1 then return end
        if map1[pos.x+2][pos.y] == 0 or map[pos.x+2][pos.y] == 2 then map2[pos.x+2][pos.y]=1 map2[pos.x+1][pos.y]=0 pos=pos+vec2(1,0) GameWinCheck() return end
    end
    if map2[pos.x+1][pos.y] ==0 then
        if map1[pos.x+1][pos.y] == 0 or map1[pos.x+1][pos.y] ==2 then pos = pos +vec2(1,0) end end
end

function MoveUp()
    if map1[pos.x][pos.y+1] == 1 then return end
    if map2[pos.x][pos.y+1] ==1 then
        if map2[pos.x][pos.y+2] ==1 or map1[pos.x][pos.y+2] == 1 then return end
        if map1[pos.x][pos.y+2] == 0 or map1[pos.x][pos.y+2] == 2 then map2[pos.x][pos.y+2] =1 map2[pos.x][pos.y+1] =0 pos=pos+vec2(0,1) GameWinCheck() return end
    end
    if map2[pos.x][pos.y+1] ==0 then
        if map1[pos.x][pos.y+1] == 0 or map1[pos.x][pos.y+1] ==2 then pos = pos +vec2(0,1) end end
end

function MoveDown()
    if map1[pos.x][pos.y-1] == 1 then return end
    if map2[pos.x][pos.y-1] ==1 then
        if map2[pos.x][pos.y-2] ==1 or map1[pos.x][pos.y-2] == 1 then return end
        if map1[pos.x][pos.y-2] == 0 or map1[pos.x][pos.y-2] == 2 then map2[pos.x][pos.y-2] = 1 map2[pos.x][pos.y-1]=0 pos=pos+vec2(0,-1) GameWinCheck() return end
    end
    if map2[pos.x][pos.y-1] ==0 then
        if map1[pos.x][pos.y-1] == 0 or map1[pos.x][pos.y-1] ==2 then pos = pos +vec2(0,-1) end end
end

function GameWinCheck()
    local count=0
    for i=1,mapwidth do
        for j=1,mapheight do
            if map2[i][j] ==1 and map1[i][j] == 2 then count=count+1 end
        end
    end
    if count == 5 then gameState=win end
end

-- 图片按钮类
-- 作者：bk194232658
-- 版本：1.0
ImgButton = class()
function ImgButton:init(x,y,img)
    self.x = x
    self.y = y
    self.width = nil --贴图绘制宽度
    self.image = img --没按下时的图片
    self.pressedImage = img --按下时的图片
    self.color = color(255, 255, 255, 255) --没按下时的图片颜色
    self.pressedColor = color(79, 100, 208, 255) --按下后的图片颜色
    self.pressed = false --是否按下中
    self.clickedEvent = nil --点击后的事件回调
    self.touchid = nil --触摸事件ID
    self.visible = true -- 是否可见
    self.enabled = true -- 是否激活
end
function ImgButton:draw()
    if self.visible == false then
        return
    end
    pushStyle()
    spriteMode(CENTER)
    if self.pressed then
        tint(self.pressedColor)
        if self.width then
            sprite(self.pressedImage,self.x,self.y,self.width)
        else
            sprite(self.pressedImage,self.x,self.y)
        end
    else
        tint(self.color)
        if self.width then
            sprite(self.image,self.x,self.y,self.width)
        else
            sprite(self.image,self.x,self.y)
        end
    end
    popStyle()
end
function ImgButton:touched(touch)
    if self.visible == false or self.enabled == false then
        return
    end
    if touch.state == ENDED and self.touchid == touch.id then
        self.pressed = false
    end --]]
    local w,h
    if self.pressed then
        w,h = spriteSize(self.pressedImage)
    else
        w,h = spriteSize(self.image)
    end
    if self.width then
        h = h * self.width / w
        w = self.width
    end
    w,h = w / 2,h / 2
    if touch.x > self.x - w and touch.y > self.y - h and touch.x < self.x + w and touch.y < self.y + h then
        if touch.state == BEGAN then
            self.pressed = true
            self.touchid = touch.id
        elseif touch.state == ENDED then
            self.pressed = false
            self.touchid = nil
            if self.clickedEvent then self.clickedEvent() end
        end
    end
end